//
//  SleepRecordViewController.swift
//  ExtremeHealth
//
//  Created by 李忠凯 on 2018/9/27.
//  Copyright © 2018年 GanDongKeJi. All rights reserved.
//

class SleepRecordViewController: SleepBaseViewController {
    @IBOutlet weak var centerButton: UIButton! {
        didSet {
            centerButton.layer.borderColor = UIColor("#27BFA1").cgColor
            centerButton.layer.borderWidth = 1
        }
    }
    @IBOutlet weak var leftButton: UIButton! {
        didSet {
            leftButton.layer.borderColor = UIColor("#27BFA1").cgColor
            leftButton.layer.borderWidth = 1
        }
    }
    @IBOutlet weak var rightButton: UIButton! {
        didSet {
            rightButton.layer.borderColor = UIColor("#27BFA1").cgColor
            rightButton.layer.borderWidth = 1
        }
    }
    @IBOutlet weak var recordView: SleepRecordView! {
        didSet {
            recordView.record = "aaaaabbacabccacbacabccc"
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class SleepRecordView: UIView {
    var record: String = ""

    enum Record {
        case low, middle, high
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        guard let sublayers = layer.sublayers else { setUp(); return }
        guard sublayers.count != 0 else { setUp(); return }
    }

    private func setUp() {
        backgroundColor = .clear
        let segments = convertRecordToSegments(record)
        let layers = convertSegmentsToLayers(segments)
        layers.forEach { [weak self] (lay) in
            self?.layer.addSublayer(lay)
        }
    }

    private func convertRecordToSegments(_ record: String) -> [Record] {
        return record.map({ (char) -> Record in
            switch char {
            case "a":
                return .low
            case "b":
                return .middle
            default:
                return .high
            }
        })
    }

    private func convertSegmentsToLayers(_ segments: [Record]) -> [CALayer] {
        let width: CGFloat = bounds.width / CGFloat(segments.count)
        var index = 0
        return segments.map({ (record) -> CALayer in
            defer { index += 1}
            var originY: CGFloat, height: CGFloat
            var startColor: UIColor, endColor: UIColor
            switch record {
            case .low:
                originY = bounds.height * 0.5
                height = bounds.height * 0.5
                startColor = UIColor("#F4BE00")
                endColor = UIColor("#DE8100")
            case .middle:
                originY = bounds.height * 0.4
                height = bounds.height * 0.6
                startColor = UIColor("#4486CA")
                endColor = UIColor("#3F44AC")
            case .high:
                originY = bounds.height * 0.3
                height = bounds.height * 0.7
                startColor = UIColor("#1DD6B5")
                endColor = UIColor("#0087B6")
            }

            let shapeLayer = CAShapeLayer()
            shapeLayer.path = UIBezierPath(rect: CGRect(
                origin: CGPoint(x: CGFloat(index) * width, y: originY),
                size: CGSize(width: width, height: height))).cgPath
            shapeLayer.fillColor = UIColor.white.cgColor

            let gradient = CAGradientLayer()
            gradient.frame = bounds
            gradient.colors = [startColor.cgColor,
                               endColor.cgColor]
            gradient.mask = shapeLayer
            return gradient
        })
    }

}
